<?php

namespace liketry\controllers;

use Yii;
use common\models\GoodsRedis;
use services\models\ProductSpec;
use services\models\ProductStock;
use services\models\Product;
use services\models\discount\FullCut;
use yii\web\NotFoundHttpException;

class GoodsdetailController extends CustomController {
    /**
     * 商品详情页
     * 
     * @return array
     */
    public function actionIndex() {
        $return_arr = GoodsRedis::getGoodsListByRedis ( $_REQUEST );
        if (! empty ( $return_arr ['error'] )) {
            throw  new NotFoundHttpException(true);
        }
        $goods_info = $return_arr ['goods_info'];
        if ($_REQUEST['product_id'] >= 20000000 && $_REQUEST['product_id'] <= 40000000)
        {
            if (isset($goods_info ['need_show']) && $goods_info ['need_show'] == 0) {
                throw  new NotFoundHttpException(true);
            } 
        } else if ($goods_info ['status'] == 0) {
            throw  new NotFoundHttpException(true);
        }
        $pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
        preg_match_all ( $pattern, $goods_info ['goods_main'], $replacement );
        foreach ( $replacement [1] as $rk => $rv ) {
            if ($_REQUEST['product_id'] >= 20000000 && $_REQUEST['product_id'] <= 40000000) {
                $font_url = \Yii::$app->params ['domains']['images.liketry.com'];
            } else {
                $font_url = \Yii::$app->params ['goods_image_url'];
            }
            $goods_info ['goods_main'] = str_replace ( $rv, $font_url . $rv, $goods_info ['goods_main'] );
        }
        $goods_info ['auto_date'] = $goods_info ['auto_time'] - time ();
        $products = $return_arr ['products'];
        $sku1 = '';
        $sku2 = '';
        // 规格
        if (! empty ( $goods_info ['sku'] [0] ['spec_id'] )) {
            // 一级SKU只要子级有库存就点亮
            $sku1 = $this->actionGetskuhtml ( $goods_info ['sku'], $products );
            if (! empty ( $goods_info ['sku'] [1] ['childs'] )) {
                $sku2 = $this->actionChidskyhtml ( $goods_info ['sku'], $products );
            }
        }
        
        $fc = new FullCut ();
        $discount_data = $fc->getDataFromRedis ();
        
        return $this->render ( 'detail', [ 
                'goods_info' => $goods_info,
                'sku1' => $sku1,
                'sku2' => $sku2,
                'uid' => $this->bestry_uid,
                'fullcut' => $discount_data ['desc'] 
        ] );
    }
    
    /**
     * 详情页切换规格检测
     * 
     * @return multitype:number string multitype: NULL
     */
    public function actionJumping() {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $data = GoodsRedis::toJumping ( $_REQUEST );
        if ($data ['flag'] == 1) {
            if (! empty ( $data ['sku'] [1] )) {
                $sku2 = $this->actionChidskyhtml ( $data ['sku'], $data ['products'] );
                $data ['desc'] = $sku2;
            } else {
                $sku2 = '没有库存';
                $data ['desc'] = $sku2;
            }
        }
        return $data;
    }
    
    /**
     * 规格HTML
     * return array
     */
    public function actionGetskuhtml($goods_info, $products) {
        $html = $this->renderPartial ( 'detail_sku', [ 
                'goods_info' => $goods_info [0],
                'products' => $products 
        ] );
        return $html;
    }
    
    /**
     *
     * @param 商品ID $goods_id            
     * @param 上级规格ID $pid            
     * @return string
     */
    public function actionChidskyhtml($goods_info, $products) {
        // 判断当前选中规格下所有商品是否有库存
        $list = ProductSpec::findProductByGoodsIdAndSpecId ( $products ['goods_id'], $products ['sku1'] );
        // 查询上级规格有货的PID
        $inarr = [ ];
        foreach ( $list as $k => $v ) {
            // 判断商品是否下架
            $isinfo = Product::findByProductId ( $v ['product_id'] );
            if (! empty ( $isinfo ['product_id'] )) {
                $thisstock = ProductStock::findByProductId ( $v ['product_id'] );
                if (! empty ( $thisstock ['stock'] ) && $thisstock ['stock'] > 0) {
                    $inarr [] = $thisstock ['product_id'];
                }
            }
        }
        // 获取当前二级规格有没有以上PID
        if (! empty ( $inarr )) {
            foreach ( $goods_info [1] ['childs'] as $k1 => $v1 ) {
                $lists = ProductSpec::findProductByGoodsIdAndSpecId ( $products ['goods_id'], $v1 ['spec_value_id'] );
                $flag = 0;
                foreach ( $lists as $k2 => $v2 ) {
                    foreach ( $inarr as $k3 => $v3 ) {
                        if (! empty ( $v2 ['product_id'] ) && $v2 ['product_id'] == $v3) {
                            $goods_info [1] ['childs'] [$k1] ['idgray'] = 1;
                            $flag = 1;
                            break;
                        } else {
                            $goods_info [1] ['childs'] [$k1] ['idgray'] = 0;
                        }
                    }
                    if ($flag == 1) {
                        break;
                    }
                }
            }
        } else {
            foreach ( $goods_info [1] ['childs'] as $k1 => $v1 ) {
                $goods_info [1] ['childs'] [$k1] ['idgray'] = 0;
            }
        }
        $html = $this->renderPartial ( 'detail_child_sku', [ 
                'goods_info' => $goods_info [1],
                'products' => $products 
        ] );
        return $html;
    }
    
    /**
     * 商品评论信息
     * 
     * @param 商品ID $goods_id            
     * @return array
     */
    public function actionGetreplymore() {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $return_arr = GoodsRedis::getReplyByRedis ( $_REQUEST );
        if (! empty ( $return_arr ['reply_info'] [0] )) {
            $html = $this->renderPartial ( 'reply_list', [ 
                    'reply_info' => $return_arr ['reply_info'],
                    'backRate' => $return_arr ['backRate'] 
            ] );
        } else {
            $html = '';
        }
        $return_arr ['text'] = $html;
        return $return_arr;
    }
}